#!/bin/bash

print_usage() {
echo "Syntax:      $0 firstrun/newpackage/editpackage"
echo 
echo "Usage Examples:-"
echo 
echo "firstrun:    $0 firstrun InstallerSourceXML \"Repository Full Name\""
echo "update:      $0 update"
echo "newpackage:  $0 newpackage packagename.zip Category Version \"Description\""
echo "editpackage: $0 editpackage packagename MaintainerName MaintainerEmail Category Version \"Description\""
}

### Vars ######
repofile=${2}
repodesc="${3}"
### END #######

### 1. Unzipping packages and creating metadata ##############################################
steps1-3_run() {
echo 1/4: Unzipping packages and creating metadata.....
mkdir -p ../unzipped/debs
cp ../scripts/*.py ../unzipped/debs/
cp ../scripts/*.xml ../unzipped/debs/
if [ ! -f ../unzipped/mpkgs ]; then
	cp ../scripts/mpkgs ../unzipped/
fi
for z in *.zip; do unzip -K -o -qq $z -d ../unzipped/debs/`basename $z .zip|tr A-Z a-z`/; done
cd ../unzipped/debs
mkdir -p ../../debug
python metadata.py $repofile > ../../debug/debug.txt

### 2. Preparing packages ########################
echo 2/4: Preparing packages....
if [ -f setperms.sh ]; then
	echo Setting package-specific perms...
	./setperms.sh
fi
rm -f *.py *.pyc *.xml *.sh
for x in *; do
	if [ -d ${x}/__MACOSX ]; then
		rm -rf ${x}/__MACOSX
	fi
	if [ -d ${x}/DEBIAN ]; then
		echo "" >> ${x}/DEBIAN/control
		chmod +x ${x}/DEBIAN/*
	fi
rm -f ${x}/*.txt
done
find . -name '.DS_Store' -exec rm -rf {} \; -prune

### 3. Building packages and cleaning up ##############
echo 3/4: Building packages and cleaning up
for x in *; do dpkg -b $x 1>/dev/null 2>/dev/null; done
ls -d * | grep -v .deb | xargs rm -rf
cd ..
}

### 4. Creating Repository Info Files ##########################################################################################################
step4b_run(){
echo 4/4: Creating Repository Info Files
chmod +x mpkgs
./mpkgs debs indices/override | awk '{gsub("Section: Network\n","Section: Networking\n");print}' > dists/stable/main/binary-darwin-arm/Packages2
cp dists/stable/main/binary-darwin-arm/Packages2 dists/stable/main/binary-darwin-arm/Packages 
gzip -9 -f dists/stable/main/binary-darwin-arm/Packages
mv dists/stable/main/binary-darwin-arm/Packages2 dists/stable/main/binary-darwin-arm/Packages
}

update_repo() {
fakeroot
steps1-3_run
rm -f dists/stable/main/binary-darwin-arm/Packages*
step4b_run
cat<<END>dists/stable/Release
`cat < dists/stable/Release | grep -v Packages.gz`
END
for file in dists/stable/main/binary-darwin-arm/Packages.gz; do echo " $(md5sum "${file}" | cut -d ' ' -f 1) $(du -b "${file}" | cut -d $'\t' -f 1) ${file}";done >> dists/stable/Release
echo Exit Status: $?
exit
}

first_run() {
fakeroot
rm -f *.done
rm -rf ../unzipped

steps1-3_run

mkdir -p indices
touch indices/override
mkdir -p dists/stable/main/binary-darwin-arm

step4b_run

cat<<END>dists/stable/Release
Origin: stable
Label: ${repodesc}
Suite: stable
Version: 1.0
Codename: stable
Architectures: darwin-arm
Components: main
Description: ${repodesc}
MD5Sum:

END
for file in dists/stable/main/binary-darwin-arm/Packages.gz; do echo " $(md5sum "${file}" | cut -d ' ' -f 1) $(du -b "${file}" | cut -d $'\t' -f 1) ${file}"; done >>dists/stable/Release
mkdir -p conf
cat<<END>conf/distributions
Origin: stable
Label: ${repodesc}
Suite: stable
Codename: stable
Architectures: darwin-arm
Components: main
Description: ${repodesc}
END
echo Exit Status: $?
exit
}

### Debicizer option selection ##
case $1 in
	--usage ) print_usage ;;
	firstrun ) first_run ;;
	update ) update_repo ;;
	newpackage ) new_package ;;
	editpackage ) edit_package ;;
	"" ) print_usage ;;
esac
### END #########################

### Not implemented ####################
corrupted_metadata(){
for c in *; do rm -f ${c}/DEBIAN/*; done
}
### END ################################
